package com.yunfeng.supercall.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.quickdev.library.adapter.ListBaseAdapter.ViewHolder;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.MyCircleBean;
import com.yunfeng.supercall.manager.ManagerFactory;

/**
 * 我加入的圈子列表
 * 
 * @author 洋
 * 
 */
public class MyCircleListActivity extends
		BaseStickyHeaderListActivity<MyCircleBean> {

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if (position == 0) {
			startActivity(new Intent(this, CreateCircleActivity.class));
		} else {
			startActivity(new Intent(this, MyCircleDetailActivity.class)
					.putExtra(KEY_DATA, mList.get(position)));
		}
	}

	@Override
	public void onSupportContentChanged() {
		// TODO Auto-generated method stub
		super.onSupportContentChanged();
		setTextViewText(R.id.title, getString(R.string.my_circle));
	}

	@SuppressWarnings("unchecked")
	@Override
	public void loadData() {
		// TODO Auto-generated method stub
		mList = (List<MyCircleBean>) ManagerFactory.createManager(MyCircleBean.class)
				.getList();
		if (mList == null) {
			mList = new ArrayList<MyCircleBean>();
		}
		String creat = getResources().getString(R.string.create_circle);
		if (!mList.isEmpty()) {
			if (!mList.get(0).getName().equals(creat)) {
				MyCircleBean myCircle = new MyCircleBean();
				myCircle.setName(creat);
				mList.add(0, myCircle);
			}
		} else {
			MyCircleBean myCircle = new MyCircleBean();
			myCircle.setName(creat);
			mList.add(0, myCircle);
		}
		setListAdapter();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_system_friends, null);
		}
		ContactsBaseBean.setupSingleLineContactsView(convertView,
				mList.get(position));
		final TextView name = ViewHolder.get(convertView, R.id.tv);
		name.setText(mList.get(position).getName());
		if (position == 0) {
			final ImageView iv = ViewHolder.get(convertView, R.id.iv);
			iv.setImageResource(R.drawable.ic_create_circle);
		} else {
			final ImageView iv = ViewHolder.get(convertView, R.id.iv);
			iv.setImageBitmap(AppContext.BITMAP);
		}
		return convertView;
	}
}
